package com.china_goldcard.paydialog;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jk on 16/11/2.
 */

public class PayWayAdapter extends RecyclerView.Adapter<PayWayViewHolder> {

    public interface OnItemClickListener{
        void onItemClick(int position,PayWayModel model);
    }

    private List<PayWayModel> data = new ArrayList<>();

    private Context context;

    private OnItemClickListener onItemClickListener;

    public PayWayAdapter(List<PayWayModel> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public PayWayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.pay_dialog_pay_item_view,parent,false);
        return new PayWayViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(PayWayViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!=null){
                    onItemClickListener.onItemClick(position,data.get(position));
                }
            }
        });
        if (data.get(position).isSelected()){
            holder.selectedIcon.setVisibility(View.VISIBLE);
        }else{
            holder.selectedIcon.setVisibility(View.GONE);
        }
        holder.textView.setText(data.get(position).getPaywayName());
        holder.imageView.setImageResource(data.get(position).getPayIcon());
        if (data.size() -1 == position){
            setMargins(holder.line,0,0,0,0);
        }
    }

    public void reloadData(List<PayWayModel> data){
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return this.data.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 设置margin
     * @param v
     * @param l
     * @param t
     * @param r
     * @param b
     */
    private static void setMargins (View v, int l, int t, int r, int b) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }
}

class PayWayViewHolder extends RecyclerView.ViewHolder{

    ImageView imageView;

    TextView textView;

    View line;

    ImageView selectedIcon;

    public PayWayViewHolder(View itemView) {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.pay_way_item_icon);
        textView = (TextView) itemView.findViewById(R.id.pay_way_item_name);
        line = (View) itemView.findViewById(R.id.pay_way_item_line);
        selectedIcon = (ImageView) itemView.findViewById(R.id.pay_way_item_selected_icon);
    }
}